<?php

namespace app\index\controller;

use think\Request;
use think\Db;

class Score extends BaseController
{
    // 积分列表
    public function index()
    {
        // 学员信息 + 查询条件
        $data = Db::table('s_score')
            ->alias('s')
            ->field([
                's.id',
                's.score',
                's.fine',
                's.ctime',
                's.info',
                'u.uname',
                'u.uclass',
                'a.name aname'
            ])
            ->join('s_user u', 's.uid = u.id')
            ->join('s_adminuser a', 'a.id = s.adminid')
            ->order([
                's.ctime' => 'DESC',
                's.id' => 'DESC'
            ])
            ->limit(100)
            ->select();
        return view('score/index', ['data' => $data]);
    }


    // 前往添加学员页面
    public function add()
    {
        return $this->success(
            '请前往学员列表，选择【学员】再【扣分】',
            'index/user/index',
            '',
            5
        );
    }


    // 修改积分的 模态框数据
    public function scorerow($id)
    {
        $row = Db::name('score')->field(true)->find($id);
        if ($row === null) {
            $res = ['code' => 4004, 'row' => '查无此数据，请检查操作或联系管理员'];
        } else {
            $res = ['code' => 200, 'row' => $row];
        }
        return $res;
    }


    // 更新积分数据
    public function update(Request $request, $id)
    {
        // 获取更新数据, 并做调整
        $put = $request->put();
        unset($put['id']);
        $put['adminid'] = session('admin.id');

        $row = Db::name('score')->where('id', $id)->update($put);
        if ($row) {
            $res = [
                'code' => 200,
                'status' => 'success',
                'msg' => "扣分信息更新成功，<a href='/score.html'>刷新页面</a> 更新数据！"
            ];
        } else {
            $res = ['code' => 4005, 'status' => 'danger', 'msg' => '更新扣分记录失败,请重试！'];
        }
        return $res;
    }


    // 删除指定积分数据
    public function delete($id)
    {
        $row = Db::name('score')->delete($id);
        if ($row) {
            $res = ['code' => 200, 'status' => 'success', 'msg' => '删除积分记录成功!'];
        } else {
            $res = ['code' => 4005, 'status' => 'warning', 'msg' => '删除积分记录失败,请重试!'];
        }
        return $res;
    }


    // 新增积分记录
    public function save(Request $request)
    {
        if (!Request::instance()->isAjax()) {
            $this->redirect('index/score/index');
            exit;
        }

        $id = $request->post('id');
        $sdata = $request->post();
        if ($id <= 0) {
            // ID 不存在 或传递有误
            $res = ['code' => 4001, 'status' => 'danger', 'msg' => '缺少关键信息，操作终止!'];
        } else {
            // 检查用户是否存在
            $row = Db::name('user')->field('uname')->where(['id' => $id])->find();
            if ($row == null) {
                $res = ['code' => 4004, 'status' => 'danger', 'msg' => '用户信息错误，操作终止!'];
            } else {
                // 存在则,记录扣分信息
                $data = [
                    'uid' => $id,
                    'score' => intval($sdata['score']),
                    'fine' => intval($sdata['fine']),
                    'ctime' => $sdata['ctime'],
                    'info' => $sdata['info'],
                    'adminid' => session('admin.id')
                ];
                // 记录扣分信息
                $result = Db::name('score')->data($data)->insert();
                if ($result == 1) {
                    $res = ['code' => 200, 'status' => 'success', 'msg' => '新增扣分记录成功!'];
                } else {
                    $res = ['code' => 4005, 'status' => 'warning', 'msg' => '新增扣分记录失败,请重试!'];
                }
            }
        }
        return $res;
    }
}
